/**
 * Created by sam on 16-10-13.
 */
var webpage = require('webpage'), page = webpage.create();
//var fs = require('fs');
var system =require('system');

page.viewportSize = { width: 414, height: 736 };
page.settings = {
    javascriptEnabled: true,
    loadImages: true,
    webSecurityEnabled: false,
   // userAgent: 'Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.137 Safari/537.36 LBBROWSER'
    userAgent: 'Mozilla/5.0 (iPhone; CPU iPhone OS 8_0 like Mac OS X) AppleWebKit/600.1.3 (KHTML, like Gecko) Version/8.0 Mobile/12A4345d Safari/600.1.4'
};

page.onLoadStarted = function () {
    page.startTime = new Date();
};

//var url = "https://www.amazon.com/s/ref=nb_sb_noss?url=search-alias%3Daps&field-keywords=iphone+7+plus+case&rh=i%3Aaps%2Ck%3Aiphone+7+plus+case";
var url = "http://item.m.jd.com/product/2138264.html";

page.open(url, function () {
    console.log('page start ----> ');
    if (status === 'fail') {
        console.log('open page fail!');
    } else {
        //waitFor(cb);
        setTimeout(function () {
            screan("3.png")

          /* var a= page.evaluate(function () {
               return document.getElementById("a-page").innerHTML;
            });*/

            console.log(page);
            page.close();
            phantom.exit();
        },5000)
    }
});

function cb() {
       var p = page.evaluate(function () {
            $('.pn-next',$("#J_bottomPage")).click();
            return $(".p-skip >em >b",$("#J_bottomPage")).text();
        });
    pageNum = parseInt(p);
    if(pageNum<max){
        max = pageNum;
    }
    //console.log("indxe:"+index+" page:"+pageNum)
    if(max<=parseInt(index)){
        var t = Date.now() - page.startTime;
        console.log('time :' + (t/1000) + '秒');
        page.close();
        phantom.exit();
    }else{
        waitFor(cb);
        index++;

    }
}


//页面截图
function screan(filename) {
   // page.render("../screan/"+filename);
    page.render(""+filename);
}

function isReday() {
    return page.evaluate(function () {
        //判断页面加载完成的信号,
        window.scrollTo(0,9999);
        return $("#J_scroll_loading span").text()==''&&$("#J_loading").html()==null;
    });
}

function readData(callback) {
    page.evaluate(function () { //操作页面事件


    });
    callback();
}

function waitFor(callback) {
    var start = new Date().getTime(),
        condition = false,
        interval = setInterval(function () {
            if ((new Date().getTime() - start < timeOutMillis) && !condition) {
                condition = isReday();
            } else {
                if (!condition) {

                    console.log("waitFor timeout|"+index);
                    page.close();
                    phantom.exit();
                } else {
                    console.log("waitFor page:"+index+" finished in " + (new Date().getTime() - start) + "ms.");
                    readData(callback);
                    clearInterval(interval);
                }
            }
        }, 250);
};

page.onConsoleMessage = function (msg) {
    console.log('CONSOLE:' + msg);
};
